package com.dms.wuziqi.controller;

import com.dms.wuziqi.JwtUtil.JwtUtil2;
import com.dms.wuziqi.Result.Result;
import com.dms.wuziqi.fliter.Role;
import com.dms.wuziqi.pojo.Loan;
import com.dms.wuziqi.service.LoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*")
@RestController
public class loanController {
    @Autowired
    LoanService loanService;
    @PostMapping("/applyLoan")
    @Role(role = "user")
    public Result applyLoan(@RequestBody Loan loan ,@RequestHeader(name = "Authorization")String token){
        Map<String,Object> claim=new HashMap<>();
        claim=JwtUtil2.parseToken(token);
        int UserId= (int) claim.get("userId");
        loanService.insertLoan(loan.getLoanUse(),loan.getLoanAccount(),new Date(),loan.getLoanDay(),UserId);
         return Result.success("申请成功");
    }

//    @GetMapping("/getLoanInfo")
//    public List<>()
//    {
//
//    }
    @GetMapping("/getLoanContract")
    public Result getLoanContract(@RequestParam int loanId){
        System.out.println(loanId);
        Loan loan=loanService.getLoanById(loanId);
        System.out.println(loan);
        return  Result.success(loan);
    }
    @GetMapping("/getAllLoan")
    @Role(role = "rooter")
    public Result getAllLoan(){

        List<Loan> loans=loanService.getAllLoan();
        return  Result.success(loans);
    }
    @Role(role = "rooter")
    @PostMapping("/auit")
    public Result auit(@RequestBody Loan loan){
        System.out.println("收到审核请求");
        System.out.println(loan);
        loanService.updateAuit(loan.getLoanId());
        return  Result.success();
    }

}
